package com.feiyu.admin.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import com.feiyu.admin.entity.AddressEntity;
import com.feiyu.admin.service.openFeign.CommonFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.feiyu.admin.entity.ShopEntity;
import com.feiyu.admin.service.ShopService;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.R;



/**
 * 
 *
 * @author wanghanbin
 * @email 1336891708@qq.com
 * @date 2021-04-08 22:35:19
 */
@RestController
public class ShopController {
    @Autowired
    private ShopService shopService;

    @Autowired
    private CommonFeignService commonFeignService;

    /**
     * 店铺信息带参查询
     */
    @PostMapping("/getShopInfoByParam")
    public R getShopInfoByParam(@RequestBody Map<String, Object> params){
        PageUtils page = shopService.queryPage(params);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("totalCount", page.getTotalCount());
        hashMap.put("pageSize", page.getPageSize());
        hashMap.put("totalPage", page.getTotalPage());
        hashMap.put("currPage", page.getCurrPage());
        List<?> pageList = page.getList();
        List<Map<String, Object>> shopInfo_list = new ArrayList<>();
        for (int i = 0; i < pageList.size(); i++) {
            HashMap<String, Object> shopInfo = new HashMap<>();
            ShopEntity shopEntity = (ShopEntity) pageList.get(i);
            AddressEntity addressEntity = new AddressEntity();
            addressEntity.setAddressId(shopEntity.getShopAddress());
            R address = commonFeignService.searchAddressById(addressEntity);
            Object addressInfo_data = address.get("data");
            Map addressInfo_data_map = (Map) JSON.toJSON(addressInfo_data);
            shopInfo.put("shopId", shopEntity.getShopId());
            shopInfo.put("userId", shopEntity.getUserId());
            shopInfo.put("shopName", shopEntity.getShopName());
            shopInfo.put("shopAvator", shopEntity.getShopAvator());
            shopInfo.put("shopLicense", shopEntity.getShopLicense());
            shopInfo.put("shopIntro", shopEntity.getShopIntro());
            shopInfo.put("shopMain", shopEntity.getShopMain());
            shopInfo.put("shopType", shopEntity.getShopType());
            shopInfo.put("shopAddress", addressInfo_data_map.get("recipientAdd"));
            shopInfo.put("shopState", shopEntity.getShopState());
            shopInfo_list.add(shopInfo);
        }
        hashMap.put("list", shopInfo_list);
        return R.ok("操作成功").put("data", hashMap);
    }


    /**
     * 店铺信息精确查询
     */
    @PostMapping("/getShopInfoById")
    public R getShopInfoById(@RequestBody() ShopEntity shopEntity){
		ShopEntity shop = shopService.getById(shopEntity.getShopId());
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setAddressId(shop.getShopAddress());
        R address = commonFeignService.searchAddressById(addressEntity);
        Object addressInfo_data = address.get("data");
        Map addressInfo_data_map = (Map) JSON.toJSON(addressInfo_data);
        Map<String,Object> hashmap = new HashMap<>();
        hashmap.put("shopId",shop.getShopId());
        hashmap.put("userId",shop.getUserId());
        hashmap.put("shopName",shop.getShopName());
        hashmap.put("shopAvator",shop.getShopAvator());
        hashmap.put("shopLicense",shop.getShopLicense());
        hashmap.put("shopIntro",shop.getShopIntro());
        hashmap.put("shopMain",shop.getShopMain());
        hashmap.put("shopType",shop.getShopType());
        hashmap.put("shopAddress",addressInfo_data_map.get("recipientAdd"));
        hashmap.put("shopState",shop.getShopState());
        return R.ok("成功").put("data", hashmap);
    }
}
